//Main C++ File
//Executes and runs the main functions of the program
//Blue Laser Games		May 3, 2011

#include "Globals.h"
#include "GameHandler.h"
#include "resource.h"
#define WIN32_LEAN_AND_MEAN

//Variables
HWND hWnd;
//Prototypes
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
HWND OpenWindow(const char* cszWinClassName, const char* cszWindowName, int nCmdShow);
//Definitions
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{	
	MSG uMsg;
	memset(&uMsg, 0, sizeof(uMsg));

	hWnd = OpenWindow("D3DWindow", "Blue Lazer Games Presents Khet", nCmdShow);
	GameHandler::Into()->Init(hWnd, hInstance,true);



	while (uMsg.message != WM_QUIT){
		while (PeekMessage(&uMsg, NULL, 0, 0, PM_REMOVE))
		{TranslateMessage(&uMsg);DispatchMessage(&uMsg);}

		GameHandler::Into()->Input();
		if(!GameHandler::Into()->Update(hWnd))
			uMsg.message = WM_QUIT;
		
		GameHandler::Into()->Render();
	}

	PostQuitMessage(0);
	UnregisterClass("D3DWindow", hInstance);
	return uMsg.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg){
	
	case WM_CLOSE:	PostQuitMessage(0);	break;
	case WM_KEYUP:	
		switch(wParam)
		{
		case VK_ESCAPE:	PostQuitMessage(0);	break;
		}
	}

	return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
HWND OpenWindow(const char* cszWinClassName, const char* cszWindowName, int nCmdShow)
{
	WNDCLASSEX winClass;
	hWnd = NULL;

	HINSTANCE hInstance = ::GetModuleHandle(NULL);
	assert(NULL != hInstance);
	HICON hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));

	winClass.lpszClassName	= cszWinClassName;
	winClass.cbSize			= sizeof(WNDCLASSEX);
	winClass.style			= CS_HREDRAW | CS_VREDRAW;
	winClass.lpfnWndProc	= WindowProc;
	winClass.hInstance		= hInstance;
	winClass.hIcon			= hIcon;
	winClass.hIconSm		= hIcon;
	winClass.hCursor		= ::LoadCursor(NULL, IDC_ARROW);
	winClass.hbrBackground	= (HBRUSH)::GetStockObject(BLACK_BRUSH);
	winClass.lpszMenuName	= NULL;
	winClass.cbClsExtra		= 0;
	winClass.cbWndExtra		= 0;

	if (!RegisterClassEx(&winClass)){return NULL;}

	hWnd = ::CreateWindowEx(NULL,cszWinClassName,
		cszWindowName,	WS_OVERLAPPEDWINDOW,
		0, 0,SCREEN_WIDTH, SCREEN_HEIGHT,
		NULL, NULL, ::GetModuleHandle(NULL), NULL);

	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);



	return hWnd;
}